home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: tjensen@ix.netcom.com (Ted Jensen)
- Newsgroups: comp.lang.c
- Subject: Re: qsort help
- Date: Wed, 03 Jan 1996 02:26:59 GMT
- Organization: Netcom
- Message-ID: <4ccpgq$qpl@ixnews8.ix.netcom.com>
- References: <4ccio7$7c0@charm.magnus.acs.ohio-state.edu>
- Reply-To: tjensen@ix.netcom.com
- NNTP-Posting-Host: pax-ca7-05.ix.netcom.com
- X-NETCOM-Date: Tue Jan 02 6:26:35 PM PST 1996
- X-Newsreader: Forte Free Agent 1.0.82
-
- xiaoyi@bmecg.bme.ohio-state.edu (Xiaoyi Wu) wrote:
-
- > int (*compar)(int *a, int *b)
- > {
- > if (*a < *b) return -1;
- > else if (*a == *b) return 0;
- > else return 1;
- > }
- >
- > int return_moved_contour(int ix, int iy)
- > {
- > int i, ac, count=0;
- > int y_coord[10]; /* stores y coordinates whose x == ix */
- >
- > for (ac=0; ac<areaindex; ac++)
- > {
- > for (i=0; i<index[ac]; i++)
- > {
- > if (xpos[ac][i] == ix) y_coord[count++] = ypos[ac][i];
- > }
- > if (count == 0) continue; /* not in this contour */
- > else {
- > qsort(y_coord, count, sizeof(int), compar);
- > i = 0;
- > while (i<count/2) {
- > if ((y_coord[i] <= iy) && (y_coord[i+1] >= iy))
- > return ac;
- > else i+=2;
- > }
- > }
- > }
- > return -1; /* cursor not within a contour, no response will be generated */
- > }
-
- The function prototype for qsort() is:
-
- void qsort(void *base, size_t nelem, size_t width,
- int(*fcmp)(const void *, const void *));
-
- That last parameter is a pointer to a function which returns an
- integer and which has two parameters of type const pointer to
- void. Your compare function must be such a function which means
- it must have a prototype:
-
- int compar(const void *p1, const void *p2);
-
- Then, on entering this function, have to cast these passed
- pointers to that type in agreement with where they actually
- point. Since in your case they point to integers in your array,
- you might do something like:
-
- int compar(const void *p1, const void *p2)
- {
- int *px1, *px2;
- px1 = (int *)p1;
- px2 = (int *)p2;
-
- and then use px1 and px2 to make the appropriate
- comparisons.
-
- I didn't go through your main() in detail, so I can't vouch that
- making this change will make your program work. But, it should
- work better than it does now!
-
- BTW, if you're interested, I have written a tutorial on pointers
- and arrays which includes a discussion of function pointers,
- particularly as they are used in this kind of circumstance. You
- can get it at one of the following locations:
-
- http://oak.oakland.edu:8080/SimTel/msdos/c/ptrtut01.zip
-
- ftp.coast.net/SimTel/msdos/c/ptrtut01.zip
-
- Hope this helps!
-
- Ted Jensen Author of PTRTUT01.ZIP
- Redwood City, CA A tutorial on C pointers and Arrays
- tjensen@ix.netcom.com Available via Simtel/msdos/c
-
-